﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace KWIS9000SERVER
{
    class KwisServer
    {
        private static TcpListener _listener;
        public static void StartServer()
        {
            System.Net.IPAddress localIPAddress = System.Net.IPAddress.Parse("127.0.0.1");
            IPEndPoint ipLocal = new IPEndPoint(localIPAddress, 13337);
            _listener = new TcpListener(ipLocal);
            _listener.Start();
            WaitForClientConnect();
        }

        private static void WaitForClientConnect()
        {
            object obj = new object();
            _listener.BeginAcceptTcpClient(new System.AsyncCallback(OnClientConnect), obj);
            Console.WriteLine("listening for connection");
        }

        private static void OnClientConnect(IAsyncResult asyn)
        {
            try
            {
                TcpClient clientSocket = default(TcpClient);
                clientSocket = _listener.EndAcceptTcpClient(asyn);
                HandleClientRequest clientReq = new HandleClientRequest(clientSocket);
                clientReq.StartClient();

                Console.WriteLine("verbonden met een nerd");
                
            }
            catch (Exception se)
            {
                throw;
            }

            WaitForClientConnect();
        }
    }


    public class HandleClientRequest
    {
        TcpClient _clientSocket;
        NetworkStream _networkStream = null;
        public HandleClientRequest(TcpClient clientConnected)
        {
            this._clientSocket = clientConnected;
        }
        public void StartClient()
        {
            _networkStream = _clientSocket.GetStream();
            WaitForRequest();
        }

        public void WaitForRequest()
        {
            byte[] buffer = new byte[_clientSocket.ReceiveBufferSize];

            _networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);
        }

        private void ReadCallback(IAsyncResult result)
        {
            NetworkStream networkStream = _clientSocket.GetStream();
            try
            {
                int read = networkStream.EndRead(result);
                if (read == 0)
                {
                    _networkStream.Close();
                    _clientSocket.Close();
                    return;
                }

                byte[] buffer = result.AsyncState as byte[];
                string data = Encoding.Default.GetString(buffer, 0, read);

                //do the job with the data here
                //send the data back to client.

                Console.WriteLine("data ontvangen door server" + data);

                Byte[] sendBytes = Encoding.ASCII.GetBytes("groetjes terug A.K.A \"PONG\"");
                networkStream.Write(sendBytes, 0, sendBytes.Length);
                networkStream.Flush();
            }
            catch (Exception ex)
            {
                throw;
            }

            this.WaitForRequest();
        }
    }
}
